package com.laihui.fragment;

import android.app.Activity;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.laihui.Constants;
import com.laihui.MyApplication;
import com.laihui.activity.R;
import com.laihui.base.BaseFragment;
import com.laihui.entity.LoanBean;
import com.laihui.model.InvestDetail;
import com.laihui.utils.WebViewManager;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;


public class DetailFragment extends BaseFragment {

    @BindView(R.id.web_view)
    WebView webView;
    @BindView(R.id.ll_container)
    LinearLayout mLlContainer;
    private String css;
    private String con;
    private String imgs;
    private Activity mActivity;
    private MyApplication application;
    LoanBean investDetail;


    @Override
    public void init() {
        mActivity = getActivity();

//        application = (MyApplication) getApplication();
//        con = getIntent().getStringExtra("con");

        WebViewManager manager = new WebViewManager(webView);
        manager.enableAdaptive();
        investDetail = (LoanBean) getArguments().getSerializable("data");
        css = "<style type=\"text/css\"> " +

                "img {" +
                "width:100%;" +
                "height:auto;" +
                "}" +


                "body {" +
                "margin-right:15px;" +
                "margin-left:15px;" +
                "margin-top:15px;" +

                "font-size:34px!important;" +
                "}" +
                "p {" +
                "size:24px!important;" +
                "}"+

                "</style>";

        //if (imgs==null){
            webView.loadDataWithBaseURL(null, "<html><header>" + css + "</header><body>" + replaceHtmlImgToAbsolutelyUrl(Constants.BASE_URL_IMG+"/",investDetail.content) + "</body></html>", "text/html", "UTF-8", null);

    }

    @Override
    public int getFragmentId() {
        return R.layout.fragment_loan_base_info;
    }

    @Override
    public Object bindFragment() {
        return this;
    }

    public static String replaceHtmlImgToAbsolutelyUrl(String baseUrl, String html){
        Pattern pattern = Pattern.compile("src\\s*=\\s*\"(.+?)\"");
        Matcher matcher = pattern.matcher(html);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String str = matcher.group(0);
            if (!str.contains("http://")){
                matcher.appendReplacement(sb,str.substring(0,5)+baseUrl+str.substring(6));
            }
        }
        matcher.appendTail(sb);
        return sb.toString();
    }




}
